using LDNS.SDK.Commands.Models;
using LDNS.Interfaces;
using System;

namespace LDNS.Commands
{
    /// <summary>
    /// 获取启动时间命令处理器
    /// 处理GET_START_TIME命令，获取DNS服务启动时间
    /// 作者：huobing
    /// </summary>
    public class GetStartTimeCommandHandler : CommandHandlerBase<GetStartTimeCommand, GetStartTimeResult>
    {
        private readonly IDnsService _dnsService;
        
        /// <summary>
        /// 命令名称
        /// </summary>
        public override string CommandName => GetStartTimeCommand.NAME;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dnsService">DNS服务实例</param>
        public GetStartTimeCommandHandler(IDnsService dnsService)
            : base()
        {
            _dnsService = dnsService;
        }
        
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="args">命令参数</param>
        /// <returns>执行结果</returns>
        public override BaseCommandResult Execute(string[] args)
        {
            try
            {
                if (_dnsService != null)
                {
                    DateTime startTime = _dnsService.GetStartTime();
                    if (startTime > DateTime.MinValue)
                    {
                        var result = CreateSuccessResult("获取启动时间成功");
                        result.StartTimeLocal = startTime;
                        return result;
                    }
                    else
                    {
                        return CreateSuccessResult("服务尚未启动");
                    }
                }
                else
                {
                    return CreateErrorResult("DNS服务实例未初始化");
                }
            }
            catch (System.Exception ex)
            {
                return CreateErrorResult($"获取启动时间失败: {ex.Message}");
            }
        }
    }
}